package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPStaticUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.databinding.ActivityUserCenterBinding;
import com.ycrlkj.android.modulescore.main.entity.User;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_USER_CENTER)
public class UserCenterActivity extends AppCompatActivity {

    private ActivityUserCenterBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityUserCenterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        toUserInfo();
    }

    public void toUserInfo() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        UserInfoFragment userInfoFragment = UserInfoFragment.newInstance();
        transaction.replace(R.id.fragment_container, userInfoFragment);
        transaction.commit();
    }

    public void toEditName(View view) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        UserNameEditFragment userNameEditFragment = UserNameEditFragment.newInstance();
        transaction.replace(R.id.fragment_container, userNameEditFragment);
        transaction.commit();
    }

    public void toEditPassword(View view) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        UserPasswordEditFragment userPasswordEditFragment = UserPasswordEditFragment.newInstance();
        transaction.replace(R.id.fragment_container, userPasswordEditFragment);
        transaction.commit();
    }

    public void toEditPhone(View view) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        UserPhoneEditFragment userPhoneEditFragment = UserPhoneEditFragment.newInstance();
        transaction.replace(R.id.fragment_container, userPhoneEditFragment);
        transaction.commit();
    }

    public void writeUserToSP(User user){
        SPStaticUtils.put("user", JSON.toJSONString(user));
    }


    @Override
    public void onBackPressed() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        Fragment current = supportFragmentManager.findFragmentById(R.id.fragment_container);

        if (current instanceof UserInfoFragment) {
            finish();
        } else {
            toUserInfo();
        }

    }

    public void handleLogout(View view) {
        XPopupUtil.showModal(this, "退出登录", new OnConfirmListener() {
            @Override
            public void onConfirm() {
                SPStaticUtils.remove("token");
                SPStaticUtils.remove("user");
                SPStaticUtils.remove("userId");
                finish();
                ARouter.getInstance().build(RoutePath.PAGE_LOGIN).navigation();
            }
        });
    }


}